<template>
  <div class="upload-container-x">
    <!--  multiple允许多张同时上传时,会报错  -->
    <el-upload
      :action="action"
      :headers="headers"
      :multiple="false"
      :limit="max"
      :before-upload="beforeUpload"
      :on-success="onSuccess"
      :on-error="onError"
      :on-remove="onRemove"
      :file-list="fileList"
      :on-exceed="onExceed"
      :disabled="disabled"
      list-type="picture-card"
      :on-preview="onPreview"
      class="image-upload"
    >
      <i slot="default" class="el-icon-plus" />
      <div slot="file" slot-scope="{file}">
        <img
          class="el-upload-list__item-thumbnail"
          :src="file.url"
        >
        <span class="el-upload-list__item-actions">
          <span
            class="el-upload-list__item-preview"
            @click="onPreview(file)"
          >
            <i class="el-icon-zoom-in" />
          </span>
          <span
            v-if="isDefault"
            class="el-upload-list__item-delete"
            @click="setDefault(file)"
          >
            <i class="el-icon-circle-check" />
          </span>
          <span
            class="el-upload-list__item-delete"
            @click="onRemove(file)"
          >
            <i class="el-icon-delete" />
          </span>
        </span>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl">
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
// import codeConfig from '@/utils/code'
export default {
  props: {
    fileList: {
      type: Array,
      default() {
        return []
      }
    },
    disabled: {
      type: Boolean,
      default: false
    },
    // -------------------------
    // 缩略图 -1表示不处理
    width: {
      type: Number,
      default: -1
    },
    height: {
      type: Number,
      default: -1
    },
    quality: {
      type: Number,
      default: 100
    },
    // bucket
    bucket: {
      type: String,
      default: ''
    },
    // 图片最大宽度
    maxWidth: {
      type: Number,
      default: 1125
    },
    // 允许上传多少个
    max: {
      type: Number,
      default: 4
    },
    // 是否显示默认按钮
    isDefault: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 上传地址, 这里不要换行写,会产生很多空格的
      action: `${process.env.VUE_APP_BASE_API}/api/upload/execute?width=${this.width}&height=${this.height}&quality=${this.quality}&bucket=${this.bucket}&max-width=${this.maxWidth}`,
      // 预览
      dialogImageUrl: '',
      dialogVisible: false,
      isHide: false
    }
  },
  computed: {
    // jwt认证需要
    headers() {
      return {
        // 'X-Admin-YANG': getToken()
        [process.env.VUE_APP_TOKEN]: getToken()
      }
    }
  },
  watch: {
    fileList(v) {
      // console.log(v)
    }
  },
  methods: {
    // 设置主图
    setDefault(file) {
      if (file && file.response && file.response.data) {
        this.$emit('setDefault', file.response.data)
      } else {
        this.$emit('setDefault', file)
      }
    },
    // 预览
    onPreview(file) {
      // this.dialogImageUrl = file.url || file.thumbnail_path
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    // 上传数量限制
    onExceed() {
      this.$message({
        message: `每次只能上传${this.max}个`,
        type: 'warning'
      })
    },
    beforeUpload(file) {
      this.$emit('beforeUpload', file)
    },
    onRemove(data) {
      this.$message({
        message: '删除成功',
        type: 'success'
      })
      this.$emit('onRemove', data)
    },
    onError(err) {
      const errMsg = err.message && JSON.parse(err.message)
      this.$message({
        message: (errMsg && errMsg.msg && `上传失败,失败原因:${errMsg.msg}`) || '上传失败',
        type: 'error'
      })
      this.$emit('onError', err)
    },
    onSuccess(response, file, fileList) {
      // console.log(fileList)
      const { code, message, data } = response
      if (code === 0) {
        this.$message({
          message: message || '上传成功',
          type: 'success'
        })
        this.$emit('onSuccess', data, fileList)
      } else {
        this.$message({
          message: (message && `上传失败,失败原因:${message}`) || '上传失败',
          type: 'error'
        })
        this.$emit('onError', file)
      }
    }
  }
}
</script>

<style scoped>
.image-upload {
  /*width: 146px;*/
}

/deep/ .el-upload-list__item.is-ready {
  /*display: none;*/
}

</style>
